<?php
/**
 *  【梦想cms】 http://www.lmxcms.com
 *
 *   前台栏目页面控制器
 */
defined('LMXCMS') or exit();
use GatewayWorker\Lib\Gateway;
class ChamAction extends HomeAction{
    public $dataModel;
    public function __construct(){
        parent::__construct();
        if($this->dataModel==null){
            $this->dataModel=new DataModel();
        }
    }
    //初始化房间
    public function createRoom()
    {
        $uid=$this->SocketData['uid'];
        $roomid=$this->dataModel->getRoomId();
        $this->dataModel->creatRoom($roomid,$uid);
    }
    //
    //todo 加入游戏房间内
    //
    //传入参数：uid roomid team
    public function join()
    {
        $uid =$this->SocketData['uid'];
        $team =$this->SocketData['team'];
        $roomid =$this->SocketData['roomid'];

        $nums=$this->dataModel->getCountTeam($roomid,$team);//获取当前组内的人数
        $roomData =$this->dataModel->getRoomData($roomid);
        $maxnum =$roomData['nums'];//当前房间最大人数

        if($nums <  $maxnum ) {
            $this->dataModel->setUserInTeam($uid, $roomid, $team); //在房间加入这个玩家的信息
            //获取当前所有玩家id
            $roomData =$this->dataModel->getRoomData($roomid);
            $alluser =$roomData['alluser'];
            $playerMag["m"] = 'addplayers';
            $playerMag['id']=$uid;
            //通知所有人
            Gateway::sendToUid($alluser, json_encode($playerMag));
        }
    }

    //*用户主动离开
    public function leave(){
        //从前端获取的信息
        $roomid=$this->SocketData['roomid'];
        $uid=$this->SocketData['uid'];
        //取出房间信息
        $roomData=$this->DataModel->getRoomData($roomid);
        //删除用户信息
        //遍历teamA
        foreach ($roomData['teamA'] as $k => $v) {
            if($k==$uid) {
                if($roomData['teamA'][$uid]['joy']=='owner'){   //如果是owner，就解散房间
                    //返回前端数据给所有人
                    $allUser=$roomData['allUser'];
                    $reData['m']="";
                    Gateway::sendToUid($allUser,json_encode($reData));
                    //解散房间
                    $roomData=$this->DataModel->clearRoomData($roomid);
                    return;
                }
                unset($roomData['teamA'][$uid]);  //删除用户
            }
        }
        //遍历teamB
        foreach ($roomData['teamB'] as $k => $v) {
            if($k==$uid){
                unset($roomData['teamB'][$uid]);   //删除用户
            }
        }
        unset($roomData['allUser'][$uid]);
        //保存数据
        $this->dataModel->setRoomData($roomid,$roomData);
        //返回给前端的数据
        $reData['m']="";
        $allUser=$roomData['allUser'];
        Gateway::sendToUid($allUser,json_encode($reData));

    }
    //*房主踢出房间
    public function getout(){
        //从前端获取的信息
        $roomid=$this->SocketData['roomid'];
        $uid=$this->SocketData['uid'];            // 想要踢人
        $out_uid=$this->SocketData['out_uid'];  //被踢的人
        //取出房间信息
        $roomData=$this->DataModel->getRoomData($roomid);
        //判断uid的权限
        $power=false;
        foreach ($roomData['teamA'] as $k => $v)
        {
            if($k==$uid)
            {
                if($roomData['teamA'][$uid]['joy']=='owner') $power=true;  //判断uid是否有权限
            }
        }
        if($power==true)
        {
            //从房间中删除该用户信息
            //遍历teamA
            foreach ($roomData['teamA'] as $k => $v) {
                if($k==$uid) {
                    unset($roomData['teamA'][$uid]);
                }
            }
            //遍历teamB
            foreach ($roomData['teamB'] as $k => $v) {
                if($k==$uid)
                    unset($roomData['teamB'][$uid]);
            }
            unset($roomData['allUser'][$out_uid]);
            $this->dataModel->setRoomData($roomid,$roomData);
        }
        else
        {
            //没有权限不做处理
            return;
        }
        //返回给前端的数据
        $reData['m']="";
        $allUser=$roomData['allUser'];
        Gateway::sendToUid($allUser,json_encode($reData));
    }

    //todo 准备
    //传入参数  roomid  team   uid
    public function ready()
    {
        $roomid =$this->SocketData['roomid'];
        $team=$this->SocketData['team'];
        $uid =$this->SocketData['uid'];
        $roomData =$this->DataModel->getRoomData($roomid);

        //设置准备
        $roomData['userData'][$team][$uid]['ready'] =1;
        $this->dataModel->setRoomData($roomid,$roomData);
        $allready =$this->dataModel->isAllReady($roomid,$team);  //todo 是否所有人都准备 true 是 false  不是
        if($allready)
        {
            //开始倒计时
        }
        else {
            //通知所有人
            $alluser = $roomData['alluser'];
            $playerMag ['m'] = 'somebodyReady';
            $playerMag['id'] = $uid;
            Gateway::sendToUid($alluser, json_encode($playerMag));
        }

    }
    public function adjustment(){
//uid 对应顺序
        $uid=$this->SocketData['uid'];
        $roomid=$this->SocketData['roomid'];
        $team=$this->SocketData['team'];
        $order=$this->SocketData['order'];
        $roomData=$this->DataModel->getRoomData($roomid);
        $uidData=$roomData['$team'][$uid];
        $fitid=$this->DataModel->getFitid($roomid,$uid);
        $fitData=$roomData["team"][$fitid];
        if($uidData['joy']=='ownr')//判断是否为房主
        {
            foreach ($roomData['teamA'] as $k=>$v){
                $roomData['teamA'][$k]['order']=$order[$k];
            }
            $this->dataModel->setRoomData($roomid,$roomData);

        }
        else
        {
            //没有权限
        }
        if($fitidData['joy']=='master')//判断是否为队长
        {
            foreach ($roomData['teamB'] as $k=>$v){
                $roomData['teamB'][$k]['order']=$order[$k];
            }
            $this->dataModel->setRoomData($roomid,$roomData);

        }

    }
    //*游戏开始
    public function startgame(){
        //从前端获取的信息
        $roomid=$this->SocketData['roomid'];
        //取出房间信息
        $roomData=$this->DataModel->getRoomData($roomid);
        //获取第一个对战的人
        $uidA="";   //teamA里第一个玩家
        $uidB="";  //teamB里第一个玩家
        $resetA=0;     //是否初始化teamA出场顺序
        $resetB=0;     //是否初始化teamB出场顺序
        foreach ($roomData['teamA'] as $k =>$v)
        {
            if(empty($v['order']))
            {
                $resetA=1;
            }
        }
        foreach ($roomData['teamB'] as $k =>$v)
        {
            if(empty($v['order']))
            {
                $resetB=1;
            }
        }
        if($resetA==1)        //如果teamA里如果有人出场顺序为空，就初始化出场顺序
        {
            $i=1;
            foreach ($roomData['teamA'] as $k =>$v)
            {
                $roomData['teamA'][$k]['order']=$i;
                $i++;
            }
        }
        if($resetB==1)          //如果teamA里如果有人出场顺序为空，就初始化出场顺序
        {
            $i=1;
            foreach ($roomData['teamB'] as $k =>$v)
            {
                $roomData['teamB'][$k]['order']=$i;
                $i++;
            }
        }
        $this->dataModel->setRoomData($roomid,$roomData);
        $roomData=$this->DataModel->getRoomData($roomid);
        foreach ($roomData['teamA'] as $k =>$v)
        {
            if($v['order']==1){
                $uidA=$k;
            }
        }
        foreach ($roomData['teamB'] as $k =>$v)
        {
            if($v['order']==1){
                $uidB=$k;
            }
        }
        //返回给前端的数据
        $reData['m']="";
        //teamA和teamB里的第一个玩家
        Gateway::sendToUid($uidA,json_encode($reData));
        Gateway::sendToUid($uidB,json_encode($reData));
    }
    //返回房间
    public function backroom(){

    }
    //断线重连
    public function reconnect(){

    }

}
?>